package com.shandao.shangxiu.interf;

import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.shandao.shangxiu.AppContext;
import com.shandao.shangxiu.bean.response.ResponseBase;
import com.shandao.shangxiu.net.ErrorHelper;
import com.shandao.shangxiu.util.Constants;
import com.shandao.shangxiu.util.Logger;

/**
 * 自定义Volley框架的回调借口
 * @param <E> 泛型类
 */
public abstract class VolleyInterface<E> {

	public Listener<E> successListener() {
		Listener<E> mListener = new Listener<E>() {

			public void onResponse(E result) {
				if (result == null) {
					onFailed("");
					return;
				}
				if (result instanceof ResponseBase) {
					// 处理用户相关返回体
					ResponseBase responseBase = (ResponseBase) result;
					if (responseBase.status.equals("00")) {
						onSuccess(result);
					} else if (responseBase.status.equals("01")) {
						onFailed("");
					}
				}
			}
		};
		return mListener;
	}

	public ErrorListener errorListener() {
		ErrorListener mErrorListener = new ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				onFailed(AppContext.getInstance()
						.getText(ErrorHelper.getMessage(error)).toString());
				if (Constants.IS_DEBUG_MODE) {
					Logger.d("VolleyInterface",
							AppContext.getInstance()
									.getText(ErrorHelper.getMessage(error))
									.toString());
				}

			}
		};

		return mErrorListener;
	}

	/**
	 * Volley成功后的回调
	 * 
	 * @param result
	 */
	public abstract void onSuccess(E result);

	/**
	 * Volley失败后的回调
	 */
	public abstract void onFailed(String errorrMsg);
}
